package com.atung.parking.config.utils;

import org.springframework.data.geo.Point;

public class DistanceUtil {
    public static double haversineDistance(Point pointA, Point pointB) {
        // 地球半径（单位：千米）
        final double R = 6371.0;


        // 点A的纬度
        double lat1 = pointA.getX();
        // 点A的经度
        double lon1 = pointA.getY();
        // 点B的纬度
        double lat2 = pointB.getX();
        // 点B的经度
        double lon2 = pointB.getY();

        // 将经纬度转换为弧度
        lat1 = Math.toRadians(lat1);
        lon1 = Math.toRadians(lon1);
        lat2 = Math.toRadians(lat2);
        lon2 = Math.toRadians(lon2);

        // 计算纬度和经度的差值
        double latDistance = lat2 - lat1;
        double lonDistance = lon2 - lon1;

        // Haversine 公式
        double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
                + Math.cos(lat1) * Math.cos(lat2)
                * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);

        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        // 返回距离（单位：千米）
        return R * c;
    }
}
